package com.feicheng.iou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.constant.StatuConstant;
import com.feicheng.iou.entity.Backview;
import com.feicheng.iou.entity.Baoming;
import com.feicheng.iou.entity.Resource;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.mapper.BackviewListMapper;
import com.feicheng.iou.mapper.BackviewMapper;
import com.feicheng.iou.mapper.ResourceMapper;
import com.feicheng.iou.pojo.BackviewUpdatePOJO;
import com.feicheng.iou.pojo.lifeCyclePOJO.BaomingAndBackviewPOJO;
import com.feicheng.iou.pojo.lifeCyclePOJO.InterviewAndBackviewPOJO;
import com.feicheng.iou.pojo.lifeCyclePOJO.ResourceAndBackviewPOJO;
import com.feicheng.iou.pojo.lifeCyclePOJO.TryAndBackviewPOJO;
import com.feicheng.iou.pojo.resourceBackviewParamPOJO.BackviewListParamPOJO;
import com.feicheng.iou.pojo.resourceBackviewParamPOJO.BackviewListReturnListPOJO;
import com.feicheng.iou.pojo.resourceBackviewParamPOJO.BackviewListReturnPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.BackviewService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
@Setter
public class BackviewServiceImpl extends ServiceImpl<BackviewMapper, Backview> implements BackviewService {


    @Autowired
    private BackviewListReturnPOJO backviewListReturnPOJO; //封装线索列表信息

    @Autowired
    private BackviewMapper backviewMapper; //回访映射

    @Autowired
    private ResourceMapper resourceMapper;   //线索映射

    @Autowired
    private BackviewListMapper backviewListMapper; //回访列表映射


    @Autowired
    private InterviewAndBackviewPOJO interviewAndBackviewPOJO; //封装接待和其回访信息

    @Autowired
    private ResourceAndBackviewPOJO resourceAndBackviewPOJO; //封装线索和其回访信息

    @Autowired
    private TryAndBackviewPOJO tryAndBackviewPOJO; //封装试听和其回访信息

    @Autowired
    private BaomingAndBackviewPOJO baomingAndBackviewPOJO;


    /**
     * @Author wuweinan
     * @Description 根据学生Id和阶段查找线索列表
     * @Date 2021-7-2 9:11
     * @param studentId
     * @return void
     */
    @Override
            //原void
    public ResultInfo selectBackviewByStudentId(Integer studentId) {
        log.info("BackviewServiceImpl的selectBackviewByStudentId方法进入");

         //根据学生Id和查找线索阶段回访列表
        QueryWrapper<Backview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId).eq("stage_id", 1).orderByDesc("ct");
        List<Backview> resourceBackview = backviewMapper.selectList(queryWrapper);

        //根据学生Id和查找接待阶段回访列表
        QueryWrapper<Backview> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("student_id", studentId).eq("stage_id", 2).orderByDesc("ct");
        List<Backview> interviewBackview = backviewMapper.selectList(queryWrapper1);

        //根据学生Id和查找回访阶段回访列表
        QueryWrapper<Backview> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("student_id", studentId).eq("stage_id", 3).orderByDesc("ct");
        List<Backview> tryBackview = backviewMapper.selectList(queryWrapper2);


        //生命周期加入第四个阶段  报名阶段
        QueryWrapper<Backview> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("student_id", studentId).eq("stage_id", 4).orderByDesc("ct");
        List<Backview> baomingBackview = backviewMapper.selectList(queryWrapper3);


        //将各自的backview信息封装在集合中
        resourceAndBackviewPOJO.setBackviewList(resourceBackview);
        interviewAndBackviewPOJO.setBackviewList(interviewBackview);
        tryAndBackviewPOJO.setBackviewList(tryBackview);
        baomingAndBackviewPOJO.setBackviewList(baomingBackview);


        log.info("BackviewServiceImpl的selectBackviewByStudentId方法返回");
        return ResultInfo.success();
    }

    /**
     * @Author wuweinan
     * @Description 添加回访信息
     * @Date 2021-7-5 9:41
     * @param backview
     * @return void
     */
    @Override
    public ResultInfo insertBackview(Backview backview) {
        log.info("BackviewServiceImpl的insertBackview方法进入");

        //插入回访信息
        Integer insertCount = backviewMapper.insertBackview(backview);

        //插入条数校验
        if(insertCount != 1){
            throw new SystemException(ReturnCode.BACKVIEW_WRONG_INSERT_COUNT);
        }

        //根据StudentId查询source_id,再更新这条backview字段的source_id()
        Integer studentId = backview.getStudentId();


        //根据学生id查询线索id
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId);
        Integer resourceId = resourceMapper.selectOne(queryWrapper).getId();


        //这条backview对象设置sourece_id字段
        backview.setSourceId(resourceId);

        //设置该名学生的sourceId字段
        backviewMapper.updateById(backview);

        //插入回访表的时候根据阶段id更新线索表的状态字段

        //获取到刚插入的回访信息
        Backview backview1 = backviewMapper.selectById(backview.getId());

        //获取到这条回访记录的线索id
        Integer sourceId = backview1.getSourceId();

        //获取到这条回访记录的阶段id
        Integer stageId = backview1.getStageId();

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", sourceId);

        //根据sourceId更新对应线索记录的状态字段
        //如果插入的是线索阶段的回访，状态推进为未到访
        if(stageId == StatuConstant.RESOURCE_STAGE_ID){
            updateWrapper.set("statu", StatuConstant.WEIDAOFANG);
        }

        //如果插入的是到访阶段的回访，状态推进为未试听
        if(stageId == StatuConstant.INTERVIEW_STAGE_ID){
            updateWrapper.set("statu", StatuConstant.WEISHITING);

        }

        //如果插入的是的试听阶段的回访，状态推进为未报名
        if(stageId == StatuConstant.TRY_STAGE_ID){
            updateWrapper.set("statu", StatuConstant.WEIBAOMING);
        }

        //更新线索表的状态字段
        resourceMapper.update(null, updateWrapper);

        log.info("BackviewServiceImpl的insertBackview方法返回");
        return ResultInfo.success();

    }

    /**
     * @Author wuweinan
     * @Description 更新一条回访信息
     * Student,Resource,Backview作为查询的对象.
     * @Date 2021-7-5 14:19
     * @param backviewUpdatePOJO
     * @return  Integer
     */
    @Override
    public ResultInfo updateBackviewById(BackviewUpdatePOJO backviewUpdatePOJO) {
        log.info("BackviewServiceImpl的updateBackviewById方法进入");

        //更新一条回访信息
        Integer integerCount = backviewMapper.updateBackview(backviewUpdatePOJO);

        //参数校验
         if(integerCount != 1){
            throw new SystemException(ReturnCode.BACKVIEW_WRONG_UPDATE_COUNT);
        }
        log.info("BackviewServiceImpl的updateBackviewById方法返回");
         return ResultInfo.success();

    }

    /**
     * @Author wuweinan
     * @Description 动态查询回访分页列表信息
     * Student,Resource,Backview作为查询的对象.
     * @Date 2021-7-5 14:19
     * @param backviewListParamPOJO
     * @return  List<Backview>
     */
    @Override
    public ResultInfo selectBackviewsPagesByConditions(BackviewListParamPOJO backviewListParamPOJO) {
        log.info("BackviewServiceImpl的selectBackviewsPagesByConditions方法进入");

        //根据BackviewListParamPOJO的字段查询出List<BackviewListReturnListPOJO>
        List<BackviewListReturnListPOJO> cs = backviewListMapper.selectBackviewsPagesByCondition(backviewListParamPOJO);



        //返回查询出的结果条数
        Integer totalCount = backviewListMapper.statisticsSelectResult(backviewListParamPOJO);


        //封装信息
        backviewListReturnPOJO.setPage(backviewListParamPOJO.getPage());
        backviewListReturnPOJO.setSize(backviewListParamPOJO.getSize());
        backviewListReturnPOJO.setTotal(totalCount);


        //计算总页数，将double转化成int
        Double doubleCount = (double)totalCount/backviewListParamPOJO.getSize();
        double ceil = Math.ceil(doubleCount);
        int totolPage = (int)ceil;


        //封装信息
        backviewListReturnPOJO.setTotalPage(totolPage);
        backviewListReturnPOJO.setList(cs);
        log.info("BackviewServiceImpl的selectBackviewsPagesByConditions方法返回");
       return ResultInfo.success(backviewListReturnPOJO);

    }

    /**
     * @Author wuweinan
     * @Description 动态查询单个回访分页信息
     * Student,Resource,Backview作为查询的对象.
     * @Date 2021-7-5 14:19
     * @param backviewListParamPOJO
     * @return  List<BackviewListReturnListPOJO>
     */
    @Override
    public ResultInfo selectBackviewPagesByConditions(BackviewListParamPOJO backviewListParamPOJO) {
       log.info("BackviewServiceImpl的selectBackviewPagesByConditions方法进入");

        //根据BackviewListParamPOJO的字段查询出List<BackviewListReturnListPOJO>
        List<BackviewListReturnListPOJO> backviewListReturnListPOJOS =
                backviewListMapper.selectBackviewPagesByCondition(backviewListParamPOJO);


        //返回查询出的结果条数
        Integer totalCount = backviewListMapper.statisticSelectResult(backviewListParamPOJO);


        //封装信息
        backviewListReturnPOJO.setPage(backviewListParamPOJO.getPage());
        backviewListReturnPOJO.setSize(backviewListParamPOJO.getSize());
        backviewListReturnPOJO.setTotal(totalCount);


        //计算总页数，将double转化成int
        Double doubleCount = (double)totalCount/backviewListParamPOJO.getSize();
        double ceil = Math.ceil(doubleCount);
        int totalPage = (int)ceil;


        //封装信息
        backviewListReturnPOJO.setTotalPage(totalPage);
        backviewListReturnPOJO.setList(backviewListReturnListPOJOS);

        log.info("BackviewServiceImpl的selectBackviewPagesByConditions方法返回");
        return ResultInfo.success(backviewListReturnPOJO);

    }
}
